// const baseURL = "http://121.40.220.156:8080";
const baseURL = "https://api.banpresto-point.com";
class Request {
    constructor(parms) {
        this.baseURL = parms.baseURL;
        this.withBaseURL = parms.withBaseURL;
    }
    request(parames) {
        let url = parames.url || "";
        let method = parames.method || "GET";
        let data = parames.data || "";
        this.withBaseURL = url.indexOf("http") == -1;
        let requestUrl = this.withBaseURL ? this.baseURL + url : url;
        let options = {
            method,
            credentials: "include",
            headers: {
                "Content-Type": "application/json; charset=UTF-8",
            },
            body: JSON.stringify(data),
        };
        if (method == "GET") {
            delete options.body;
            let qs = "?";
            for (const key in parames.data) {
                qs += key + "=" + parames.data[key] + "&";
            }
            qs = qs.substring(0, qs.length - 1);
            qs.length > 1 ? (requestUrl += qs) : "";
        }
        return fetch(requestUrl, options)
            .then(function (response) {
                return response.json();
            })
            .then(function (res) {
                return successFn(res);
            })
            .catch(function (err) {
                error(err);
            });
    }
}
function error(err) {
    console.log("网络不给力！请稍后再试");
}
function successFn(data) {
    switch (data.code) {
        case 0:
            return data.data;
            break;
        default:
            businessError(data.msg);
            break;
    }
}
function businessError(params) {
    console.log(params);
}
const request = new Request({
    baseURL: baseURL,
    withBaseURL: true,
});

export default request;
